package com.tgy.helloworld.AOP;

import com.tgy.helloworld.entity.Entity;
import com.tgy.helloworld.utils.Tool;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/***
 * @ClassName: AopTest
 * @Description: 测试aop
 * @Auther: 送你一块二向箔
 * @Date: 17:12 2022/4/23
 * @version : V1.0
 */
@Aspect//标注这个类是一个切面
@Component //注册到spring中管理
public class AopTest {
    @Resource
    Tool tool;
    @Before("execution(* com.tgy..AOP.*Test.*(..))")
    public void before(){
        tool.test();
        System.out.println("---------方法执行前AnnotationPointcut---------");

    }

    @After("execution(* com.tgy.helloworld.AOP.AopServiceTest.*(..))")
    public void after(){
        System.out.println("---------方法执行后AnnotationPointcut---------");
    }

    //在环绕增强中，我们可以给定一个参数，代表我们要获取处理的切入点
    @Around("execution(* com.tgy.helloworld.AOP.AopServiceTest.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
/*        System.out.println("环绕前AnnotationPointcut");
        System.out.println("签名:"+jp.getSignature());
        //执行目标方法proceed
        Object proceed = jp.proceed();
        System.out.println("环绕后AnnotationPointcut");
        System.out.println(proceed);*/

        List<Entity> arg = (List<Entity>)jp.getArgs()[0];
        arg.forEach((bean)->{
            String name = bean.getName();
            if (!StringUtils.isEmpty(name) &&name.matches(".*\\/*")){
                bean.setName(name.split("/")[0]);
            }
        });
        //arg.remove(0);

        Object [] aaa = {arg};

        System.out.println("==============");
        System.out.println("获取参数"+arg.toString());
        Object proceed = jp.proceed(aaa);
        System.out.println("==============");

    }
}
